package seismosurfer.gui.panel;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;

import seismosurfer.data.constants.ParameterNames;
import seismosurfer.gui.GUIConstants;
import seismosurfer.layer.SiteLayer;

import com.bbn.openmap.MapBean;
import com.bbn.openmap.event.ProjectionEvent;
import com.bbn.openmap.event.ProjectionListener;
import com.bbn.openmap.gui.OMComponentPanel;
import com.bbn.openmap.proj.Projection;

/**
 * This panel has controls to set the criteria (e.g. population)
 * to choose sites.
 *
 */
public class SitesPanel extends OMComponentPanel implements ProjectionListener,
        ParameterNames, GUIConstants {
    
    private static final long serialVersionUID = 2575646383820959443L;

    private SiteLayer sl = null;

    protected Projection p;    

    JSpinner popMin;

    JSpinner popMax;

    public SitesPanel(ActionListener buttonsListener) {
        super();
        createPanel(buttonsListener);
    }

    public void sendQueryParameters() {

        Map parameters = new HashMap();

        parameters.put(POPULATION_MIN, (Integer) (popMin.getValue()));
        parameters.put(POPULATION_MAX, (Integer) (popMax.getValue()));

        if (sl != null) {
            sl.loadSites(parameters);
        } else {
            System.out.println("NULL Layer!!!");
        }

    }

    protected void createPanel(ActionListener buttonsListener) {

        JPanel mainPanel = new JPanel();
        GridBagLayout mainPanelLayout = new GridBagLayout();
        mainPanel.setLayout(mainPanelLayout);

        JPanel xPanel = new JPanel();
        GridBagLayout xPanelLayout = new GridBagLayout();
        xPanel.setLayout(xPanelLayout);

        JPanel selectPanel = new JPanel();
        GridBagLayout selectPanelLayout = new GridBagLayout();
        selectPanel.setLayout(selectPanelLayout);

        JLabel selectLabel = new JLabel("Select sites with population");
        selectPanel.add(selectLabel, new GridBagConstraints(1, 0, 3, 1, 0.0,
                0.0, GridBagConstraints.WEST, GridBagConstraints.NONE,
                new Insets(11, 32, 0, 116), 6, 15));

        JLabel betweenLabel = new JLabel("between");
        selectPanel.add(betweenLabel, new GridBagConstraints(0, 1, 1, 1, 0.0,
                0.0, GridBagConstraints.WEST, GridBagConstraints.NONE,
                new Insets(16, 11, 10, 0), 8, 5));

        SpinnerNumberModel snm = new SpinnerNumberModel(10000, 0, 100000000, 1); // (initial
        // value,
        // min,
        // max,
        // step)
        popMin = new JSpinner(snm);
        selectPanel.add(popMin, new GridBagConstraints(1, 1, 1, 1, 1.0, 1.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
                        15, 12, 10, 0), 87, 1));

        JLabel andLabel = new JLabel("and");
        selectPanel.add(andLabel, new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0,
                GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(
                        16, 21, 10, 0), 1, 7));

        snm = new SpinnerNumberModel(1000000, 0, 100000000, 1); // (initial
        // value, min,
        // max, step)
        popMax = new JSpinner(snm);
        selectPanel.add(popMax, new GridBagConstraints(3, 1, 1, 1, 1.0, 1.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
                        15, 15, 10, 10), 81, 1));

        JPanel buttonsPanel = new JPanel();
        FlowLayout btnsLayout = new FlowLayout();
        buttonsPanel.setLayout(btnsLayout);

        JButton okBtn = new JButton("OK");
        okBtn.addActionListener(buttonsListener);
        okBtn.setActionCommand(OK);
        buttonsPanel.add(okBtn, null);

        JButton cancelBtn = new JButton("Cancel");
        cancelBtn.setActionCommand(CANCEL);
        cancelBtn.addActionListener(buttonsListener);
        buttonsPanel.add(cancelBtn, null);

        xPanel.setMaximumSize(new Dimension(377, 290));

        selectPanel.setMaximumSize(new Dimension(377, 85));
        selectPanel.setMinimumSize(new Dimension(377, 85));
        selectPanel.setOpaque(true);
        selectPanel.setPreferredSize(new Dimension(377, 85));

        xPanel.add(selectPanel, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
                        0, 0, 0, 0), 1, 7));

        buttonsPanel.setMaximumSize(new Dimension(139, 36));

        mainPanel.add(xPanel, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
                        0, 0, 0, 2), 0, 0));

        mainPanel.add(buttonsPanel, new GridBagConstraints(0, 1, 1, 1, 1.0,
                1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                new Insets(6, 0, 13, 2), 238, -1));

        add(mainPanel, BorderLayout.CENTER);
    }

    public void projectionChanged(ProjectionEvent e) {
        p = e.getProjection();        
    }

    public void findAndInit(Object someObj) {

        if (someObj instanceof SiteLayer) {
            sl = (SiteLayer) someObj;
        }

        if (someObj instanceof MapBean) {
            MapBean map = (MapBean) someObj;
            map.addProjectionListener(this);

            p = map.getProjection();

        }

    }    

    public void findAndUndo(Object someObj) {

    }

}
